<?php

namespace App\V3\V3_2_7\Models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class BarterGoodsDetailModel extends Model
{


    use HasFactory;
    protected $table = 'barter_goods_detail';

    protected $dateFormat = 'U';//这个时间字段被为被格式化为UNIX 时间戳的形式存储

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    /**
     * 查询换货商品信息
     * @param unknown $userId
     * @param unknown $data
     * @return unknown
     */
    static public function getGoodsDetailInfo($userId, $data)
    {
        return SELF::select('barter_id as id')
        ->from('barter_goods_detail as a')
        ->leftJoin('materiel_type as b','b.id','=','a.type_id')
        ->where([
            'a.main_id' => $data['main_id'],
            'a.delete_flag' => 0
        ])
        ->where(function($query) use ($data) {
            if($data['search_type'] == 1){
                $query->where('b.materiel_name', 'like', '%'.$data['keyword'].'%');
            }else{
                $query->where('b.materiel_no', 'like', '%'.$data['keyword'].'%');
            }
        })->get()->toArray();
    }

    /**
     * 换货商品列表
     * @param unknown $sellId
     * @return unknown
     */
    static public function detail($barterId)
    {
        return self::select(
            'barter_goods_detail.id',
            'barter_goods_detail.id as barter_goods_id',
            'barter_goods_detail.type_id',
            'a.materiel_name',
            'a.type_name',
            'a.materiel_no',
            'a.materiel_img',
            'price',
            'barter_goods_detail.num',
            'barter_goods_detail.out_num',
            'b.storage_name',
            'b.id as storage_id',
            'piece',
            'barter_id',
            'cost_price',
            'barter_goods_detail.shelf_id',
            'barter_goods_detail.location_id',
            's.shelf_name',
            'l.location_name',
            'a.is_use_serial',
            'a.materiel_class_id',
            'a.materiel_unit',
            'a.is_product_materiel',
            'a.is_use_serial',
            'a.texture',
            'a.executive_standard',
            'barter_goods_detail.remark',
            'barter_goods_detail.batch_info'
            )
            ->where('barter_id', $barterId)
            ->where('barter_goods_detail.delete_flag',0)
            ->where('a.delete_flag',0)
            ->where('b.delete_flag',0)
            ->where('s.status',1)
            ->where('l.status',1)
            ->leftJoin('materiel_type as a', 'a.id', '=', 'barter_goods_detail.type_id')
            ->leftJoin('storage as b', 'b.id', '=', 'barter_goods_detail.storage_id')
            ->leftJoin('storage_shelf as s','barter_goods_detail.shelf_id','=','s.id')
            ->leftJoin('storage_shelf_location as l','barter_goods_detail.location_id','=','l.id')
            ->get()->toArray();
    }
}
